/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import entidad.*;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import javax.persistence.*;
import javax.swing.JOptionPane;

/**
 *
 * @author Juan
 */
public class MovieDAO {

    public boolean crear(Movie movie) {
        boolean respuesta=false;
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try {
            em.persist(movie);
            em.getTransaction().commit();
            respuesta=true;
        } catch (Exception e) {
            em.getTransaction().rollback();
            JOptionPane.showMessageDialog(null, e);
        } finally {
            em.close();
            return respuesta;
        }
    }
    
     public List<Movie> buscarPorGenero(String genre){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        try{
        Query q=em.createQuery("SELECT m from Movie m where m.genre like :genre").setParameter("genre",genre);
        return (List<Movie>) q.getResultList();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
     
    public Director consultarDirector(Integer id){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        try{
        Query q=em.createQuery("SELECT m from Director as m where m.movie.id like :id").setParameter("id",id.toString());
        return (Director)q.getSingleResult();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
    
    public List<Actor> consultarActores(Integer id){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        try{
        Query q=em.createQuery("SELECT m from Actor m join m.movies p WHERE p.id like :id").setParameter("id",id.toString());
        return (List<Actor>)q.getResultList();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
    
    public List<Artist> consultarArtistas(Integer id){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        try{
        Query q=em.createQuery("SELECT DISTINCT m from Artist m join m.songs p join p.soundTrack q join q.movie r where r.id like :id").setParameter("id",id.toString());
        return (List<Artist>)q.getResultList();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }

    public List<Movie> buscar(String nombre){
        JOptionPane.showMessageDialog(null, nombre);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        em.getTransaction().begin();
        try{
        Query q=em.createQuery("SELECT m from Movie m where lower(m.name) like lower(:nombre)").setParameter("nombre","%"+nombre+"%");
        return (List<Movie>) q.getResultList();
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
    
    public List<Movie> buscarPorDirector(String nombre){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = new ArrayList<Movie>();
        List<Director> directors = new ArrayList<Director>();
        try{
        Query q=em.createQuery("SELECT m from Director m where lower(m.name) like lower(:nombre) or lower(m.lastName) like lower(:nombre)").setParameter("nombre","%"+nombre+"%");
        directors = (List<Director>)q.getResultList();
        for(Director a: directors){
            movies.add(a.getMovie());
        }
        return movies;
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
    
     public List<Movie> buscarPorActor(String nombre){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = new ArrayList<Movie>();
        List<Movie> moviesT = new ArrayList<Movie>();
        List<Actor> actors = new ArrayList<Actor>();
        try{
        //Case sensitive Query, needs to be replaced.
        Query q=em.createQuery("SELECT m from Actor m where lower(m.name) like lower(:nombre )or lower(m.lastName) like lower(:nombre)").setParameter("nombre","%"+nombre+"%");
        actors = (List<Actor>)q.getResultList();
        for(Actor a: actors){
            movies=a.getMovies();
            for(Movie b: movies){
                moviesT.add(b);
            }
        }
        
        HashSet hs = new HashSet();
        hs.addAll(moviesT);
        moviesT.clear();
        moviesT.addAll(hs);
        return moviesT;
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
    }
    
     public List<Movie> buscarPorArtista(String nombre){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        List<Movie> movies = new ArrayList<Movie>();
        List<Artist> artists = new ArrayList<Artist>();
        List<SoundTrack> soundTracks = new ArrayList<SoundTrack>();
        try{
        //Case sensitive Query, needs to be replaced.
        Query q=em.createQuery("SELECT m from Artist m where lower(m.name) like lower(:nombre) or lower(m.lastName) like lower(:nombre)").setParameter("nombre","%"+nombre+"%");
        artists  = (List<Artist>)q.getResultList();
        for(Artist a: artists){
            for(Song b : a.getSongs()){
                soundTracks.add(b.getSoundTrack());
            }
        }  
        HashSet hs = new HashSet();
        hs.addAll(soundTracks);
        soundTracks.clear();
        soundTracks.addAll(hs);
        
        for(SoundTrack c: soundTracks){
            movies.add(c.getMovie());
        } 
        return movies;
        }
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
            return null;
        }
        
    }
     
    public void editMovie (String nombre, float duracion, String Genero, float rating,int año, int id){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAPracticePU");
        EntityManager em = emf.createEntityManager();
        Movie movie = em.find(Movie.class, id);
        em.getTransaction().begin();
       try{
          movie.setGenre(Genero);
          movie.setDuration(duracion);
          movie.setName(nombre);
          movie.setRating(rating);
          movie.setYearMovie(año);
          em.getTransaction().commit();
          JOptionPane.showMessageDialog(null, "Edición exitosa");
      }   
        catch(Exception e){
            JOptionPane.showMessageDialog(null, e);
        }
    }
}
